1. Field of the Invention
The present invention relates to a computer program product, system, and method for managing a lock to a resource shared among a plurality of processors.
2. Description of the Related Art
Locks are used in shared memory parallel programs executed in a multi-processor environment to provide mutual exclusivity for non-parallelized sections of the code that may be executed by only one processor at a time. One prior art lock is a spin lock, such as a Test and Set lock. A first user that acquires the lock can access the non-parallelized code and other processes must continually check, i.e., spin, the lock to determine when the lock is available. The first processor that checks after the lock becomes available may take the lock even if other processors have been waiting a longer time.
Another type of lock is an array based queue lock algorithm. Each lock requires a counter and spin locations, or cache line, for each processor. The counter values provide addresses to the slots. Upon initialization of the lock, the spin locations, also known as slots, are set to busy, except the first location, slot 0, is set free, and a counter having values corresponding to the slots is set to 0. The first processor seeking the lock performs an atomic fetch_and_increment operation on the counter to access the counter value and then increment the counter value. The fetched counter value provides the address or number of the slot that the processor is to access, or spin on.
The requesting processor checks the counter having the zero value and accesses the first slot, corresponding to the zero counter value. If the slot is free, then the processor has acquired the lock and will then set the slot to busy, for the next use of the slot. If the slot is not free, then the requesting processor spins on that slot until it is free. When a processor releases a lock, the next slot is set to free.
This array based queue lock uses one cache line for each slot so that each processor may spin on that slot until the slot is set to free. This technique may require significant memory resources as the number of locks increases, because for each lock, a cache line is required for each slot, or the number of processors.
There is a need in the art for improved techniques for managing locks to shared resources.